package view;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;

import org.primefaces.context.RequestContext;

import controller.EmployeeController;
import entity.Association;
import entity.BankPayMethod;
import entity.Commissioned;
import entity.Contractor;
import entity.Employee;
import entity.PayMethod;
import entity.Salaried;
import entity.SendMailPayMethod;
import entity.Union;

@ManagedBean
@ViewScoped
public class EditEmployeeView {

	@Inject
	EmployeeController employeeController;

	private Employee employee;
	private float union_tax;
	private float sales_percent;
	private float monthly_salary;
	private float HourlyRate;
	private String address;
	private String iban;
	private String message = "Dati non modificati";
	private String visibFragment="";
	private PayMethod method;
	private Association association;
	@PostConstruct
	public void ServiceView() {
		FacesContext context = FacesContext.getCurrentInstance();
		String id = context.getExternalContext().getRequestParameterMap()
				.get("SendEmployee");
		this.employee = employeeController.findEmployee(Integer.parseInt(id));
		this.method=employee.getPayMethod();
		this.association=employee.getAssociation();

		
	}

	public void updateEmployee() {
		Employee existEmployee = employeeController.findEmployee(employee
				.getId());
		employee.setAssociation(association);
		employee.setPayMethod(method);
		if (existEmployee.getUsername().compareTo(employee.getUsername()) != 0) {
			if (employeeController.checkUsername(employee)) {
				employeeController.updateEmployee(employee);
				
				RequestContext.getCurrentInstance().execute(
						"PF('dialogYes').show();");
			} else {
				message = "Username already exists";
				RequestContext.getCurrentInstance().execute(
						"PF('dialogNo').show();");
			}
		} else if (existEmployee.getCodfisc().compareTo(employee.getCodfisc()) != 0) {
			if (employeeController.checkCodFisc(employee)) {
				employeeController.updateEmployee(employee);
				RequestContext.getCurrentInstance().execute(
						"PF('dialogYes').show();");
			} else {
				message = "Code already exists";
				RequestContext.getCurrentInstance().execute(
						"PF('dialogNo').show();");
			}
		} else {
			employeeController.updateEmployee(employee);
			RequestContext.getCurrentInstance().execute(
					"PF('dialogYes').show();");
		}

	}
	
	public void instancePayMethod() throws Exception {
		if (visibFragment != "") {
			Class<?> myClass = Class.forName("entity." + visibFragment);
			 this.method=(PayMethod) myClass.newInstance();
			 this.method.setEmployee(employee);
		}else{
			this.method=null;
		}
	}
	
	public void DeleteAssociation(){
		int value=employeeController.deleteAssociation(employee);
		if(value==0){
			RequestContext.getCurrentInstance().execute("PF('dialogYes').show();");
		}else{
			RequestContext.getCurrentInstance().execute("PF('dialogNo').show();");
		}
	}
	public void deletePaymethod(){
		int value=employeeController.deletePayMethod(employee);
		if(value==0){
			RequestContext.getCurrentInstance().execute("PF('dialogYes').show();");
		}else{
			RequestContext.getCurrentInstance().execute("PF('dialogNo').show();");
		}
	}
	public void AddAssociation(){
		this.association=new Union();
	}
	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	public float getUnion_tax() {
		return ((Union) this.association).getUnion_tax();
	}

	public void setUnion_tax(float union_tax) {
		((Union) this.association).setUnion_tax(union_tax);
		;
	}

	public float getSales_percent() {
		return ((Commissioned) employee).getSales_percent();
	}

	public void setSales_percent(float sales_percent) {
		((Commissioned) this.employee).setSales_percent(sales_percent);
	}

	public float getMonthly_salary() {
		return ((Salaried) employee).getMonthly_salary();
	}

	public void setMonthly_salary(float monthly_salary) {
		((Salaried) employee).setMonthly_salary(monthly_salary);
	}

	public float getHourlyRate() {
		return ((Contractor) employee).getHourly_rate();
		
	}

	public void setHourlyRate(float hourlyRate) {
		((Contractor) employee).setHourly_rate(hourlyRate);
	}

	public String getIban() {
		return ((BankPayMethod)method).getIban();
	}

	public void setIban(String iban) {
		((BankPayMethod)method).setIban(iban);;
	}

	public String getMessage() {
		return message;
	}
	public void setAddress(String address) {
		((SendMailPayMethod)method).setAddress(address);
	}

	public String getAddress() {
		return ((SendMailPayMethod)method).getAddress();
	}
	

	public void setMessage(String message) {
		this.message = message;
	}


	public String getVisibFragment() {
		return visibFragment;
	}

	public void setVisibFragment(String visibFragment) {
		this.visibFragment = visibFragment;
	}

	public PayMethod getMethod() {
		return method;
	}

	public void setMethod(PayMethod method) {
		this.method = method;
	}

	public Association getAssociation() {
		return association;
	}

	public void setAssociation(Association association) {
		this.association = association;
	}
	

}
